package com.gopay.bis.spdb.ebank.operation;

import java.util.HashMap;
import java.util.Map;

import com.gopay.bis.common.util.XMLBeanUtils;
import com.gopay.bis.spdb.ebank.operation.format.Packet;
import com.gopay.common.domain.bank.spdb.ebank.SpdbMessage;

/**
 * 构建浦发银行对账交易所需信息
 * 
 * @SpdbBuildMessage.java
 * @author wanggang
 * @2013-12-5 上午11:14:29 www.gopay.com.cn Inc.All rights reserved.
 */
public class SpdbBuildMessage {

	private final static String XML_HEADER = "<?xml version=\"1.0\" encoding=\"GBK\" ?>";

	/**
	 * 构建对账签名所需文件格式
	 * 
	 * @param message
	 * @return stirng
	 */
	public String buildQueryMessage(SpdbMessage message) {
		StringBuffer sb = new StringBuffer();
		sb.append(getCheckXML(message));
		return sb.toString();
	}

	/**
	 * 根据message组装spdb所需报文格式xml
	 * 
	 * @param message
	 * @return
	 */
	private String getCheckXML(SpdbMessage msg) {
		// 排列按TX的属性排列顺序
		StringBuffer sb = new StringBuffer();
		sb.append(XML_HEADER);
		@SuppressWarnings("rawtypes")
		Map<String, Class> cm = new HashMap<String, Class>();
		cm.put("packet", Packet.class);
		Packet pack = null;
		String xml = null;
		try {
			pack = getPackInfo(msg);
			xml = XMLBeanUtils.bean2xml(cm, pack);
		} catch (Exception ex) {
		}
		return sb.append(xml).toString();
	}

	/**
	 * 根据SpdbMessage 组装所需类
	 * 
	 * @param msg
	 * @return
	 */
	private Packet getPackInfo(SpdbMessage msg) {
		Packet pack = new Packet();
		pack.setTransName(msg.getTransName());
		pack.setPlain(msg.getPlainMessage());
		pack.setSignature(msg.getSignMessage());
		return pack;
	}
}
